package com.enqualcomm.kids.mvp.userinfo;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.enqualcomm.kids.R;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private String[] strings;
    private LayoutInflater inflater;
    OnItemClickListener listener;

    public MyAdapter(LayoutInflater inflater, OnItemClickListener listener) {
        this.inflater = inflater;
        this.listener = listener;
    }

    public void changeData(String[] strings) {
        this.strings = strings;
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView tv;
        View divider;
        OnItemClickListener listener;

        public ViewHolder(View itemView, OnItemClickListener listener) {
            super(itemView);
            this.listener = listener;
            this.tv = (TextView) itemView.findViewById(R.id.city_name_tv);
            this.divider = itemView.findViewById(R.id.divider);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                listener.onClick(v, getLayoutPosition());
            }
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_choose_city, null);
        return new ViewHolder(view, listener);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tv.setText(strings[position]);
        if (position==strings.length-1){
            holder.divider.setVisibility(View.INVISIBLE);
        }else{
            holder.divider.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return strings == null ? 0 : strings.length;
    }
}